package org.hackystat.ui.chart.jfc.chartresource;

import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

/**
 * Holds requests for the plot rendering and sends chart file names back to the client.
 *
 * @author Pavel Senin.
 *
 */
public class ChartResource
    extends Resource {

  /** The telemetry type. */
  private String telemetryType;

  /** The telemetry interval start. */
  private String startTime;

  /** The telemetry interval end. */
  private String endTime;

  /**
   * Provides the following representational variants: TEXT_XML.
   *
   * @param context The context.
   * @param request The request object.
   * @param response The response object.
   */
  public ChartResource(Context context, Request request, Response response) {
    super(context, request, response);
    this.telemetryType = (String) request.getAttributes().get("telemetrytype");
    this.startTime = (String) request.getAttributes().get("startTime");
    this.endTime = (String) request.getAttributes().get("endTime");
    getVariants().clear(); // copied from BookmarksResource.java, not sure why needed.
    getVariants().add(new Variant(MediaType.TEXT_PLAIN));
  }

  /**
   * Returns the representation of the User resource.
   *
   * @param variant The representational variant requested.
   * @return The representation.
   */
  @Override
  public Representation getRepresentation(Variant variant) {
    Representation result = null;
    ChartManager manager = (ChartManager) getContext().getAttributes().get("ChartManager");
    if (manager == null) {
      throw new RuntimeException("Can not get from context RequestServerChartFactory");
    }
    if (variant.getMediaType().equals(MediaType.TEXT_PLAIN)) {
      try {
        result = new StringRepresentation(manager.renderChart(this.telemetryType, this.startTime,
            this.endTime));
      }
      catch (ChartResourceException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        result = null;
      }
    }
    return result;
  }

  /**
   * Indicate the DELETE method is supported.
   *
   * @return True.
   */
  @Override
  public boolean allowDelete() {
    return false;
  }

  // /**
  // * Implement the DELETE method that deletes an existing User given their email.
  // * Does not matter if the User current exists or not.
  // */
  // @Override
  // public void delete() {
  // UserManager manager = (UserManager)getContext().getAttributes().get("UserManager");
  // manager.deleteUser(email);
  // getResponse().setStatus(Status.SUCCESS_OK);
  // }

  /**
   * Indicate the POST method is supported.
   *
   * @return True.
   */
  @Override
  public boolean allowPost() {
    return false;
  }

  // /**
  // * Implement the POST method that updates the properties associated with a user.
  // * <ul>
  // * <li> The User must be currently defined in this UserManager.
  // * <li> The payload must be an XML representation of a Properties instance.
  // * </ul>
  // * @param entity The entity to be posted.
  // */
  // @Override
  // public void post(Representation entity) {
  //
  // ChartManager manager = (ChartManager)getContext().getAttributes().get("ChartManager");
  //
  // // Return failure if the User doesn't exist.
  // if (!manager.hasChart(this.chartType)) {
  // getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Unknown Chart type: " +
  // this.chartType);
  // return;
  // }
  //
  // manager.renderChart(this.chartType);
  //
  // // Update the existing property list with these new properties.
  // for (Property property : newProperties.getProperty()) {
  // user.getProperties().getProperty().add(property);
  // }
  // getResponse().setStatus(Status.SUCCESS_OK);
  // }

  // /**
  // * Instantiates the server which will listen for the rendering requests.
  // *
  // * @param portNum the port to listen to.
  // */
  // public RequestServer(Integer portNum) {
  // super();
  // super.getServers().add(Protocol.HTTP, portNum);
  // // Create a new charting Restlet
  // Restlet restlet = new Restlet() {
  // @Override
  // public void handle(Request request, Response response) {
  // // Print the requested URI path
  // String message = "Resource URI : " + request.getResourceRef() + '\n' + "Root URI : "
  // + request.getRootRef() + '\n' + "Routed part : "
  // + request.getResourceRef().getBaseRef() + '\n' + "Remaining part: "
  // + request.getResourceRef().getRemainingPart();
  // response.setEntity(message, MediaType.TEXT_PLAIN);
  // }
  // };
  // super.getDefaultHost().attach("/chart", restlet);
  // }

}
